<?php
$loader->requireOnce('includes/Datasource_sql.class.php');
$loader->requireOnce('ordo/Ticket.class.php');
class Surgery extends ORDataObject {
	var $surgery_id			=	'';
	var $medical_record_id  =   '';
	var $date				=	'';
	var $diagnosis			=	'';
	var $anesthesia			=	'';
	var $surgical_happening	=	'';
	var $surgical_method	=	'';
	var $surgery_service_id	=	'';
	var $description		=	'';

	var $_table				=	'surgery';
	var $_key				=	'surgery_id';
	var	$_internalName		=	'Surgery';

	public function Surgery(){
		parent::ORDataObject();
	}

	public function setup($id = 0){
		$this->set('id',$id);
		if($id>0)
		$this->populate();
	}

	public function populate(){
		parent::populate('surgery_id');
	}

	public function setData($recordId, $date, $diagnosis, $anesthesia,
	$happening, $method, $surgery_service_id, $description){
		$data = array(
			'medical_record_id' => $recordId,
			'date'				=> $date,
			'diagnosis'			=> $diagnosis,
			'anesthesia'		=> $anesthesia,
			'surgical_happening'=> $happening,
			'surgical_method'	=> $method,
			'surgery_service_id'=> $surgery_service_id,
			'description'		=> $description
		);
		$this->populateArray($data);
	}

	public function getId(){
		return $this->surgery_id;
	}

	public function setId($id){
		$this->surgery_id=$id;
	}

	public function getDate(){
		return $this->getLocalizedDate('date');
	}

	public function getAnesthesia(){
		return $this->anesthesia;
	}

	public function getDiagnosis(){
		return $this->diagnosis;
	}

	public function getMethod(){
		return $this->surgical_method;
	}

	public function getHappening(){
		return $this->surgical_happening;
	}

	public function getDescription(){
		return $this->description;
	}

	public function getMedicalRecordId() {
		return $this->medical_record_id;
	}

	public function getSurgeryServiceId(){
		return $this->surgery_service_id;
	}

	public function delete($surgeryId) {
		// delete from surgery_participant
		$sql = "DELETE FROM surgery_participant WHERE surgery_id = $surgeryId";
		$this->_execute($sql);

		// delete from surgery
		$sql = "DELETE FROM surgery WHERE surgery_id = $surgeryId";
		$this->_execute($sql);
	}
	public function getSurgeryObjectFromRecordId($recordId) {
		$sql = "SELECT surgery_id FROM $this->_table WHERE medical_record_id = $recordId";
		$res = $this->_execute($sql);
		$surObj = Celini::newORDO('Surgery', $res->fields['surgery_id']);
		return $surObj;
	}

    public function getSurgeryEmrDetailInfo($departmentId, $inPatientEmrId) {
        $field        =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
        EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_SURGERY);

        $ds = new Datasource_sql();
        $sql = array (
            'cols'  => "sur.surgery_id, sur.surgery_service_id service_id,
                s.$field name, sur.date date, iti.discount, iti.original_price, 
                iti.insurance_price, iti.patient_price ",
            'from'      => "$this->_table sur 
                        INNER JOIN surgery_service ss  
                            ON (sur.medical_record_id = '$inPatientEmrId' AND sur.surgery_service_id = ss.service_id)
                        INNER JOIN service s ON (s.service_id=ss.service_id)
                        INNER JOIN in_patient_emr ipe ON (ipe.in_patient_emr_id = sur.medical_record_id)
                        INNER JOIN ticket t ON (ipe.ticket_id = t.ticket_id AND t.department_id = '$departmentId')
                        INNER JOIN in_ticket_item iti ON (iti.enum_item_type = '$itemType' AND iti.item_id = sur.surgery_id)",
            'where'     => "s.item_type='service'",
            'orderby'   => "sur.date DESC"
            );
          
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            foreach ($result as &$row) {
                $row['participants'] = array();
                $participantObj = Celini::newOrdo('SurgeryParticipant');
                $list = $participantObj->getParticipantsDetailInfoBySurgeryId($row['surgery_id']);
                if ($list != null && !empty($list)) {
                   $row['participants'] = $list; 
                }
                                
            } 
             
            
                      
            return $result;
    }

	public function getSurgeryServiceObject(){
		$obj	=	Celini::newORDO('SurgeryService',$this->surgery_service_id);
		return $obj;
	}

	public function getSurgeryPatientInfo($departmentId, $surgeryDateFrom, $surgeryDateTo) {
		$cols = 's_m.surgery_id,
				person.name,
				patient.patient_code,
				department.name as dept,
				date_format(s_m.date, "%d/%m/%Y") as surgeryDate';

		$whereCond = '';
		if ($departmentId != '' && $departmentId != 'all') {
			$whereCond = " and ticket.department_id=$departmentId";

		}

		$subQuery = "select surgery.surgery_id, surgery.date, medical_record.ticket_id
			         from surgery
			              INNER JOIN medical_record on surgery.medical_record_id=medical_record.medical_record_id";

		$sql = array(
			'cols' => $cols,
			'from' => " ticket
							INNER JOIN patient ON ticket.patient_id = patient.person_id
							INNER JOIN person ON ticket.patient_id = person.person_id
							INNER JOIN department ON ticket.department_id = department.department_id
							INNER JOIN ($subQuery) as s_m ON  ticket.ticket_id=s_m.ticket_id",

			'where' => " date(s_m.date) <= date('$surgeryDateTo')
						 and date('$surgeryDateFrom') <= date(s_m.date) "
		. $whereCond,

			'orderby' => 'department.name asc'
			);
			$ds = new Datasource_sql();
			$ds->setup($this->_db, $sql);
			return $ds->toArray();

	}

    public function deleteSurgery($surgeryId = 0){
        $sql    =   "delete from $this->_table where surgery_id=$surgeryId";
        $this->_execute($sql);
    }

    public function getSurgeryInfoByPatient($patientId) {
        $field        =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $dateFormat =   L10nHelper::getSqlDateFormat();
        $itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
            EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_SURGERY);
        $status     =   Ticket::OPEN_STATUS;

        $ds = new Datasource_sql();
        $sql = array (
            'cols'  => "sur.surgery_id, sur.surgery_service_id service_id,
                s.$field name, DATE_FORMAT(iti.date, '$dateFormat') date , d.$field department_name, 
                iti.discount, iti.original_price, iti.insurance_price, iti.patient_price ",
            'from'      => "$this->_table sur
                        LEFT JOIN surgery_service ss  ON (sur.surgery_service_id = ss.service_id)
                        LEFT JOIN service s ON (s.service_id=ss.service_id)
                        RIGHT JOIN in_ticket_item iti ON (iti.enum_item_type = '$itemType' AND iti.item_id = sur.surgery_id)
                        INNER JOIN ticket t ON (iti.ticket_id = t.ticket_id)
                        LEFT JOIN department d ON t.department_id = d.department_id",
            'where'     => "s.item_type='service' AND t.patient_id='$patientId' AND t.status='$status'",
            'orderby'   => "sur.date DESC"
            );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        foreach ($result as &$row) {
            $row['participants'] = array();
            $participantObj = Celini::newOrdo('SurgeryParticipant');
            $list = $participantObj->getParticipantsDetailInfoBySurgeryId($row['surgery_id']);
                if ($list != null && !empty($list)) {
                    $row['participants'] = $list;
                }
        }

        return $result;
    }
   
}
?>